import unittest
from time import sleep

import requests

from common.resd_json import read_json
from common.utils_assert import common_assert
from api.RegLoginApi import RegLoginApi
from common.DButil import clear_user_info
from parameterized import parameterized


class TestRegLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # 清洗数据
        clear_user = clear_user_info()

    def setUp(self) -> None:
        self.ses = requests.session()
        self.reg_login_api = RegLoginApi(self.ses)

    def tearDown(self) -> None:
        self.ses.close()

    # 获取图片验证码测试用例
    @parameterized.expand(read_json("test_data", "api1_data"))
    def test01_img_verify_code(self, r, expect_status_code):
        resp = self.reg_login_api.get_img_verify_code(r)
        self.assertEqual(expect_status_code, resp.status_code)

    # 获取短信验证码测试用例
    @parameterized.expand(read_json("test_data", "api2_data"))
    def test02_phone_verify_code(self, phone, imgVerifyCode, type, expect_status_code, expect_status, expect_des):
        self.reg_login_api.get_img_verify_code(0.123)
        resp = self.reg_login_api.get_phone_verify_code(phone, imgVerifyCode, type)
        common_assert(self, resp, expect_status_code, expect_status, expect_des)

    # 注册测试用例
    @parameterized.expand(read_json("test_data", "api3_data"))
    def test03_register(self, phone, password, verifycode, phone_code, dy_server, invite_phone, expect_status_code,
                        expect_status, expect_des):
        self.reg_login_api.get_img_verify_code(0.123)
        self.reg_login_api.get_phone_verify_code(phone, "8888", "reg")
        resp = self.reg_login_api.user_register(phone, password, verifycode, phone_code, dy_server, invite_phone)
        common_assert(self, resp, expect_status_code, expect_status, expect_des)

    # 登录测试用例
    @parameterized.expand(read_json("test_data", "api4_data"))
    def test04_user_login(self, keywords, password, expect_status_code, expect_status, expect_des):
        resp = self.reg_login_api.user_login(keywords, password)
        common_assert(self, resp, expect_status_code, expect_status, expect_des)

    @unittest.skip
    # 由于每次都要等60秒，为了不妨碍，跳过该条用例
    # 登录密码错误校验测试用例
    def test05_pwd_error(self):
        # 第一次失败
        resp1 = self.reg_login_api.user_login("18373444511", "11222M")
        common_assert(self, resp1, 200, 100, "密码错误1次")
        # 第二次失败
        resp2 = self.reg_login_api.user_login("18373444511", "11222M")
        common_assert(self, resp2, 200, 100, "密码错误2次")
        # 第三次失败
        resp3 = self.reg_login_api.user_login("18373444511", "11222M")
        common_assert(self, resp3, 200, 100, "账号已被锁定")
        # 等待60秒
        sleep(60)
        # 重新登陆成功
        resp4 = self.reg_login_api.user_login("18373444511", "11111MM")
        common_assert(self, resp4, 200, 200, "成功")
